// 导入 express 模块
const express = require('express');
const cors = require('cors');
const multer = require('multer');
const path = require('path');
const userController = require('../back/controller/userConntroller');
const dishController = require('../back/controller/DishController')
const manController = require('../back/controller/ManController')
const orderController=require('../back/controller/OrderController')
const businessController=require('../back/controller/BusinessController')
// 创建 express 应用
const app = express();
app.use(cors());
app.use(express.json());
// 定义一个简单的路由
app.get('/api/data', (req, res) => {
    const data = [
        {id: 1, name: 'Item 1'},
        {id: 2, name: 'Item 2'},
        {id: 3, name: 'Item 3'},
    ];
    res.json(data);
});
app.get('/getAllUser', (req, res) => {
    userController.getUserById(1, (err, user) => {
        if (err) {
            res.status(500).json({error: err.message});
        } else {
            res.json(user);
        }
    });
});
app.post('/dish/save', async (req, res) => {
    try {
        const savedDish = await dishController.saveDish(req);
        res.json(savedDish);
    } catch (error) {
        console.error("Error saving dish:", error);
        res.status(500).json({error: "Error saving dish"});
    }
});
app.get('/dish/findPage', async (req, res) => {
    try {
        const Page = await dishController.findPage(req);
        res.json(Page);
    } catch (error) {
        console.error("Error saving dish:", error);
        res.status(500).json({error: "Error saving dish"});
    }
});
app.post('/dish/delete', async (req, res) => {

    try {
        const item = req.body;
        console.log(item)
        const response = await dishController.deleteDish(item);
        res.json();
    } catch (error) {
        console.error("Error saving dish:", error);
        res.status(500).json({error: "Error delete dish"});
    }
});
app.post('/dish/delete/batch', async (req, res) => {
    try {
        const items = req.body;
        const response = await dishController.batchDish(items)
        res.json(response);
    } catch (error) {
        console.error("Error saving dish:", error);
        res.status(500).json({error: "Error batch dish"});
    }
})

// 配置Multer来处理文件上传
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'uploads/');
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
    }
});

const upload = multer({storage: storage});

// 处理POST请求，上传图片
app.post('/upload-image', upload.single('image'), (req, res) => {
    if (!req.file) {
        return res.status(400).json({error: 'No file uploaded.'});
    }

    const imageUrl = 'http://localhost:3000/' + req.file.filename;

    res.json({imageUrl: imageUrl});
});

// 设置静态文件目录
app.use(express.static('uploads'));
app.post('/man/save', async (req, res) => {
    try {
        const saveMan = await manController.saveMan(req);
        res.json(saveMan);
    } catch (error) {
        console.error("Error saving man:", error);
        res.status(500).json({error: "Error saving man"});
    }
});
app.get('/man/findPage', async (req, res) => {
    try {
        const Page = await manController.findPage(req);
        res.json(Page);
    } catch (error) {
        console.error("Error :man/findPage", error);
        res.status(500).json({error: "Error man/findPage"});
    }
});
app.delete('/man/delete/:id', async (req, res) => {
    const {id} = req.params;

    try {
        await manController.deleteMan(req);
        // 返回成功响应
        res.json({success: true});
    } catch (error) {
        console.error('删除失败', error);
        res.status(500).json({error: '删除失败'});
    }
})
app.post('/man/delete/batch', async (req, res) => {
    try {
        const items = req.body;
        console.log(items)
        const response = await manController.batchMan(items)
        res.json(response);
    } catch (error) {
        console.error("Error saving dish:", error);
        res.status(500).json({error: "Error batch dish"});
    }
})
app.post('/order/save',async (req,res)=>{
    try {
        const saveMan = await orderController.saveOrder(req);
        res.json(true);
    }catch (error){
        res.json(false);
    }
})
app.get('/order/findPage', async (req, res) => {
    try {
        const Page = await orderController.findPage(req);
        res.json(Page);
    } catch (error) {
        console.error("Error :man/findPage", error);
        res.status(500).json({error: "Error man/findPage"});
    }
})
app.delete('/order/delete/:id', async (req, res) => {
    const {id} = req.params;

    try {
        await orderController.deleteOrder(req);
        // 返回成功响应
        res.json({success: true});
    } catch (error) {
        console.error('删除失败', error);
        res.status(500).json({error: '删除失败'});
    }
})
app.post('/order/delete/batch', async (req, res) => {
    try {
        const items = req.body;
        console.log(items)
        const response = await orderController.batchOrder(items)
        res.json(true);
    } catch (error) {
        res.json(false);
    }
})
app.get('/business/getBusinessData',async (req,res)=>{
    try {
        const response = await businessController.getBusinessData(req)
        res.json(response);
    } catch (error) {
        res.json(false);
    }
})
app.get('/business/findPage',async (req,res)=>{
    try {
        const response = await businessController.findPage(req)
        res.json(response);
    } catch (error) {
        console.log(error)
        res.json(false);
    }
})
app.get('/business/getTodayData',async (req,res)=>{
    try {
        const response = await businessController.getTodayData(req)
        res.json(response);
    }catch (error){
        console.log(error)
        res.json(false);
    }
})
app.get('/business/getYearData',async (req,res)=>{
    try {
        const response = await businessController.getDataByYear(req)
        res.json(response);
    }catch (error){
        console.log(error)
        res.json(false);
    }
})
app.get('/business/getEmployeeAttendanceData',async (req,res)=>{
    try {
        const response = await businessController.getEmployeeAttendanceData(req)
        res.json(response);
    }catch (error){
        console.log(error)
        res.json(false);
    }
})
app.get('/business/getOption',async (req,res)=>{
    try {
        const response = await businessController.getOption(req)
        res.json(response);
    }catch (error){
        console.log(error)
        res.json(false);
    }
})
// 启动服务器并监听指定端口
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});